package com.example.automotive.handler;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.automotive.MainActivity;

public class SensorHandler implements SensorEventListener, LocationListener {

    private static final String TAG = "SensorHandler"; // 定义日志标签
    private final MainActivity mainActivity; // 定义 MainActivity 实例
    private final SensorManager sensorManager; // 定义 SensorManager 实例
    private final Sensor accelerometer; // 定义加速度计传感器实例
    private final Sensor gyroscope; // 定义陀螺仪传感器实例

    private LocationManager locationManager;

    @Override
    public void onLocationChanged(@NonNull Location location) {
        mainActivity.sensorData.put("latitude", location.getLatitude());
        mainActivity.sensorData.put("longitude", location.getLongitude());
        mainActivity.sensorData.put("altitude", location.getAltitude());
        mainActivity.sensorData.put("speed", location.getSpeed());
        mainActivity.sensorData.put("location_timestamp", System.currentTimeMillis());
        Log.d(TAG, "latitude: " + location.getLatitude() + ", longitude: " + location.getLongitude()
                + ", altitude: " + location.getAltitude()
                + ", speed: " + location.getSpeed());
    }

    public SensorHandler(MainActivity mainActivity) { // SensorHandler 构造函数
        this.mainActivity = mainActivity; // 初始化 MainActivity 实例
        this.sensorManager = (SensorManager) mainActivity.getSystemService(Context.SENSOR_SERVICE); // 初始化 SensorManager 实例
        this.accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); // 获取加速度计传感器实例
        this.gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); // 获取陀螺仪传感器实例

        // 请求传感器和位置权限
        if (ContextCompat.checkSelfPermission(mainActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(mainActivity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 102);
        }

        // 初始化位置服务
        locationManager = (LocationManager) mainActivity.getSystemService(Context.LOCATION_SERVICE);
        if (locationManager != null) {
            if (ActivityCompat.checkSelfPermission(mainActivity, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(mainActivity, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
            }
        }

    }

    // 注册传感器监听器
    public void registerListeners() {
        sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); // 注册加速度计传感器监听器
        sensorManager.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_NORMAL); // 注册陀螺仪传感器监听器
    }

    // 注销传感器监听器
    public void unregisterListeners() {
        sensorManager.unregisterListener(this); // 注销传感器监听器
    }

    @Override
    public void onSensorChanged(SensorEvent event) { // 当传感器数据改变时调用
        long timestamp = System.currentTimeMillis(); // 获得当前时间戳
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { // 判断是否为加速度计数据
            float x = event.values[0]; // 获取 x 轴数据
            float y = event.values[1]; // 获取 y 轴数据
            float z = event.values[2]; // 获取 z 轴数据
            mainActivity.sensorData.put("accelerometer_x", x);
            mainActivity.sensorData.put("accelerometer_y", y);
            mainActivity.sensorData.put("accelerometer_z", z);
            mainActivity.sensorData.put("accelerometer_timestamp", timestamp);
            Log.d(TAG, "AccelerometerData - x: " + x + ", y: " + y + ", z: " + z); // 记录加速度计数据日志
        } else if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) { // 判断是否为陀螺仪数据
            float x = event.values[0]; // 获取 x 轴数据
            float y = event.values[1]; // 获取 y 轴数据
            float z = event.values[2]; // 获取 z 轴数据
            mainActivity.sensorData.put("gyroscope_x", x);
            mainActivity.sensorData.put("gyroscope_y", y);
            mainActivity.sensorData.put("gyroscope_z", z);
            mainActivity.sensorData.put("gyroscope_timestamp", timestamp);
            Log.d(TAG, "GyroscopeData - x: " + x + ", y: " + y + ", z: " + z); // 记录陀螺仪数据日志
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) { // 当传感器精度改变时调用
        // 这里不处理
    }
}
